package com.example.administrator.ygddriver.into.register.forget.reset;

import android.content.Intent;
import android.support.v7.widget.CardView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.into.register.RegisterActivity;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

import java.util.HashMap;

/**
 * 密码重置界面
 */
public class ResetActivity extends BaseActivity implements View.OnClickListener {
    private SimpleToolbar simpleToolbar;
    private ImageView mPassWordDelIv, mEyesIv;
    private EditText mPasswordEt;
    private RelativeLayout resetRl;
    private String phone;

    @Override
    public int initLayout() {
        return R.layout.activity_reset;
    }

    @Override
    public void initView() {
        simpleToolbar = findViewById(R.id.activity_reset_stb);
        mPassWordDelIv = findViewById(R.id.activity_reset_password_del_iv);
        mEyesIv = findViewById(R.id.activity_reset_eyes_iv);
        mPasswordEt = findViewById(R.id.activity_reset_password_et);
        resetRl = findViewById(R.id.activity_reset_over_cv);
        resetRl.setOnClickListener(this);
        mEyesIv.setOnClickListener(this);
        mPassWordDelIv.setOnClickListener(this);
    }

    @Override
    public void initDate() {
        setSimpleToolbar();
        addEt();
        phone = getIntent().getStringExtra("phone");
    }

    private void addEt() {
        mPasswordEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPassWordDelIv.setVisibility(View.VISIBLE);
                mEyesIv.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(mPasswordEt.getText())) {
                    mPassWordDelIv.setVisibility(View.GONE);
                    mEyesIv.setVisibility(View.GONE);
                } else {
                    mPassWordDelIv.setVisibility(View.VISIBLE);
                    mEyesIv.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mPasswordEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (TextUtils.isEmpty(mPasswordEt.getText())) {
                        mPassWordDelIv.setVisibility(View.GONE);
                    } else {
                        mPassWordDelIv.setVisibility(View.VISIBLE);
                    }
                } else {
                    mPassWordDelIv.setVisibility(View.GONE);
                }
            }
        });
    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("密码重置");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.u126);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ResetActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_reset_eyes_iv:
                mPasswordEt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                break;
            case R.id.activity_reset_password_del_iv:
                mPasswordEt.setText("");
                break;
            case R.id.activity_reset_over_cv:
                if (TextUtils.isEmpty(mPasswordEt.getText().toString())) {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(ResetActivity.this, "提示", "密码不能为空");
                    baseHint.show();
                } else if (mPasswordEt.getText().length() < 6) {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(ResetActivity.this, "提示", "密码不能小于六位");
                    baseHint.show();
                } else {
                    initUpdatePasswordHtml(phone, mPasswordEt.getText().toString());

                }


                break;
        }

    }

    //忘记密码 更新密码
    private void initUpdatePasswordHtml(String phone, String password) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_account", phone);
        hashMap.put("c_password", password);
        hashMap.put("c_type", "1");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE + UrlValuse.UPDATE_PASSWORD, hashMap, ResetRequestBean.class, new OkGoRequests.OnTrue<ResetRequestBean>() {
            @Override
            public void hasData(ResetRequestBean date) {
                if (date.isSuccess()) {
                    Intent intent = new Intent(ResetActivity.this, RegisterActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(ResetActivity.this, "提示", date.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<ResetRequestBean>() {
            @Override
            public void noData() {

            }
        });

    }
}
